package com.caigou.controller;

import com.caigou.annotation.OperLog;
import com.caigou.bean.cms.param.*;
import com.caigou.bean.exam.entity.Users;
import com.caigou.constant.NewsConstant;
import com.caigou.enums.ResponseEnum;
import com.caigou.pojo.ArticleData;
import com.caigou.pojo.ArticleExportList;
import com.caigou.pojo.ArticleList;
import com.caigou.security.utils.CurrentUser;
import com.caigou.service.ArticleService;
import com.caigou.service.ArticleTitleService;
import com.caigou.util.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Api(value = "新闻类管理器")
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Resource
    private ArticleService articleService;// 新闻Service
    @Resource
    private ArticleTitleService articleTitleService;

    @ApiOperation("获取文章某个频道的发布状态")
    @RequestMapping("/loadArticleReleaseChannelStatus")
    public Result loadArticleReleaseChannelStatus(@RequestBody ArticleParam param, @CurrentUser Users users){
        return articleService.loadArticleReleaseChannelStatus(param);
    }

    @OperLog(operModule = NewsConstant.ARTICLE_MODULE, operType = NewsConstant.ADD_OPERATION, operDesc = "增加稿件")
    @ApiOperation("保存文章数据(主数据)")
    @RequestMapping("/saveArticleData")
    @PreAuthorize("hasAnyAuthority('xinjianneirongjifabu')")
    public Result saveArticleData(@RequestBody ArticleParam param, @CurrentUser Users users) {
        if (articleService.loadArticleExist(param.getArticleId()) > 0) return Result.error(ResponseEnum.ARTICLE_EXIST);
        return articleService.saveArticleData(param, users);
    }

    @ApiOperation("获取文章列表（文章列表/我的稿件/待我审核/回收站）")
    @RequestMapping("/loadArticleList")
    @PreAuthorize("hasAnyAuthority('gaojiankuchaxun')")
    public Result<PageInfo<ArticleList>> loadArticleList(@RequestBody ArticleListParam param, @CurrentUser Users users) {
        return articleService.loadArticleList(param, users);
    }

    @OperLog(operModule = NewsConstant.ARTICLE_MODULE, operType = NewsConstant.OUTPUT_OPERATION, operDesc = "导出稿件")
    @ApiOperation("获取文章导出数据列表（只有大列表可进行导出操作）")
    @RequestMapping("/loadArticleExportList")
    @PreAuthorize("hasAnyAuthority('daochutongjishuju')")
    public Result<List<ArticleExportList>> loadArticleExportList(@RequestBody ArticleListParam param, @CurrentUser Users users) {
        return articleService.loadArticleExportList(param, users);
    }

    @ApiOperation("获取文章修改页数据")
    @RequestMapping("/loadArticleData")
    @PreAuthorize("hasAnyAuthority('xiugaineirongjigengxin','gaojiankuchaxun')")
    public Result<ArticleData> loadArticleData(@RequestBody ArticleDataParam param) {
        return articleService.loadArticleData(param);
    }

    @ApiOperation("保存文章附件列表数据(单个--预设为修改页可调用)")
    @RequestMapping("/saveArticleAdjunct")
    @PreAuthorize("hasAnyAuthority('xinjianneirongjifabu','xiugaineirongjigengxin')")
    public Result saveArticleAdjunct(@RequestBody ArticleAdjunctDataParam param, @CurrentUser Users users) {
        // 验证文章ID是否存在
        if (articleService.loadArticleExist(param.getArticleId()) == 0)
            return Result.error(ResponseEnum.ARTICLE_NOT_EXIST);
        return articleService.saveArticleAdjunct(param, users);
    }

    @ApiOperation("删除文章附件列表数据(单个--预设为修改页可调用)")
    @RequestMapping("/deleteArticleAdjunct")
    @PreAuthorize("hasAnyAuthority('shanchuneirong','xinjianneirongjifabu','xiugaineirongjigengxin')")
    public Result deleteArticleAdjunct(@RequestBody ArticleAdjunctDataParam param) {
        return articleService.deleteArticleAdjunct(param);
    }

    @ApiOperation("保存文章标题列表数据(单个--预设为修改页可调用)")
    @RequestMapping("/saveArticleTitle")
    @PreAuthorize("hasAnyAuthority('xinjianneirongjifabu')")
    public Result saveArticleTitle(@RequestBody ArticleTitleDataParam param) {
        // 验证文章ID是否存在
        if (articleService.loadArticleExist(param.getArticleId()) == 0)
            return Result.error(ResponseEnum.ARTICLE_NOT_EXIST);
        // 标题类型重复验证
        if (articleTitleService.loadArticleTitleExist(param.getArticleId(), param.getArticleTitleTypeValue()) > 0)
            return Result.error(ResponseEnum.ARTICLE_TITLE_EXIST);
        return articleService.saveArticleTitle(param);
    }

    @ApiOperation("删除文章标题列表数据(单个--预设为修改页可调用)")
    @RequestMapping("/deleteArticleTitle")
    public Result deleteArticleTitle(@RequestBody ArticleTitleDataParam param) {
        return articleService.deleteArticleTitle(param);
    }

    @ApiOperation("保存词条(结构),修改页词条结构的新增，只增加结构不增加内容，内容从大表单修改接口进行填充")
    @RequestMapping("/saveArticleEntry")
    public Result saveArticleEntry(@RequestBody ArticleLawEntryParam param) {
        // 验证文章ID是否存在
        if (articleService.loadArticleExist(param.getArticleId()) == 0)
            return Result.error(ResponseEnum.ARTICLE_NOT_EXIST);
        return articleService.saveArticleEntry(param);
    }

    @ApiOperation("删除词条结构及数据，并且删除所有下属层级结构及数据")
    @RequestMapping("/deleteArticleEntry")
    public Result deleteArticleEntry(@RequestBody ArticleLawEntryParam param) {
        return articleService.deleteArticleEntry(param);
    }

    @ApiOperation("保存注释轴结构数据")
    @RequestMapping("/saveArticleSpecialAnnotation")
    public Result saveArticleSpecialAnnotation(@RequestBody ArticleSpecialAnnotationDataParam param) {
        return articleService.saveArticleSpecialAnnotation(param);
    }

    @ApiOperation("删除注释轴结构数据")
    @RequestMapping("/deleteArticleSpecialAnnotation")
    public Result deleteArticleSpecialAnnotation(@RequestBody ArticleSpecialAnnotationDataParam param) {
        return articleService.deleteArticleSpecialAnnotation(param);
    }

    @ApiOperation("保存图集数据(修改页单个保存)")
    @RequestMapping("/saveArticleImage")
    public Result saveArticleImage(@RequestBody ArticleImageParam param) {
        return articleService.saveArticleImage(param);
    }

    @ApiOperation("删除图集数据(修改页单个删除)")
    @RequestMapping("/deleteArticleImage")
    public Result deleteArticleImage(@RequestBody ArticleImageParam param) {
        return articleService.deleteArticleImage(param);
    }

    @OperLog(operModule = NewsConstant.ARTICLE_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "更新稿件")
    @ApiOperation("更新文章主数据")
    @RequestMapping("/updateArticleData")
    @PreAuthorize("hasAnyAuthority('xiugaineirongjigengxin')")
    public Result updateArticleData(@RequestBody ArticleParam param, @CurrentUser Users users) {
        return articleService.updateArticleData(param, users);
    }

    @OperLog(operModule = NewsConstant.ARTICLE_MODULE, operType = NewsConstant.DELETE_OPERATION, operDesc = "删除稿件")
    @ApiOperation("删除文章数据（单个/批量删除，逗号拼接唯一ID）(状态删除)")
    @RequestMapping("/deleteArticleData")
    @PreAuthorize("hasAnyAuthority('shanchuneirong')")
    public Result deleteArticleData(@RequestBody ArticleParam param, @CurrentUser Users users) {
        return articleService.deleteArticleData(param, users);
    }

    @ApiOperation("删除文章数据（我的稿件)")
    @RequestMapping("/deleteMineArticleData")
    public Result deleteMineArticleData(@RequestBody ArticleParam param, @CurrentUser Users users) {
        return articleService.deleteMineArticleData(param, users);
    }

    @OperLog(operModule = NewsConstant.ARTICLE_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "下架稿件")
    @ApiOperation("更新文章发布状态--审核")
    @RequestMapping("/updateArticleStatus")
    @PreAuthorize("hasAnyAuthority('xiugaineirongjigengxin')")
    public Result updateArticleStatus(@RequestBody ArticleAuditDataParam param, @CurrentUser Users users) {
        return articleService.updateArticleStatus(param, users);
    }

    @ApiOperation("音频/播报新增页--获取最大期数ID")
    @RequestMapping("/loadArticleMaxAudioIssue")
    public Result loadArticleMaxAudioIssue(@RequestBody ArticleInfoParam articleInfoParam) {
        return articleService.loadArticleMaxAudioIssue(articleInfoParam);
    }

    @ApiOperation("法律筛选面板--获取文章列表")
    @RequestMapping("/loadLawArticleList")
    @PreAuthorize("hasAnyAuthority('gaojiankuchaxun')")
    public Result loadLawArticleList(@RequestBody LawArticleListParam param) {
        return articleService.loadLawArticleList(param);
    }

    @ApiOperation("文章搜索接口--与列表接口查询条件--回参不同")
    @RequestMapping("/loadArticleListBySelect")
    @PreAuthorize("hasAnyAuthority('gaojiankuchaxun')")
    public Result loadArticleListBySelect(@RequestBody ArticleListParam param, @CurrentUser Users users) {
        return articleService.loadArticleListBySelect(param, users);
    }

    @ApiOperation("获取文章的发布位置信息")
    @RequestMapping("/loadArticleReleaseList")
    @PreAuthorize("hasAnyAuthority('gaojiankuchaxun')")
    public Result loadArticleReleaseList(@RequestBody ArticleDataParam param, @CurrentUser Users users) {
        return articleService.loadArticleReleaseList(param, users);
    }

    @ApiOperation("获取文章可关联采编人员列表")
    @RequestMapping("/loadArticlePromoterList")
    @PreAuthorize("hasAnyAuthority('gaojiankuchaxun')")
    public Result loadArticlePromoterList() {
        return articleService.loadArticlePromoterList();
    }

    @ApiOperation("获取文章操作历史记录列表")
    @RequestMapping("/loadArticleHistory")
    @PreAuthorize("hasAnyAuthority('gaojiankuchaxun')")
    public Result loadArticleHistory(@RequestBody ArticleListParam param) {
        return articleService.loadArticleHistory(param);
    }

    @ApiOperation("上传单个附件接口")
    @RequestMapping("/uploadAttachment")
    @PreAuthorize("hasAnyAuthority('xinjianneirongjifabu','xiugaineirongjigengxin')")
    public Result uploadAttachment(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "uid") String uid, @CurrentUser Users users) {
        return articleService.uploadAttachment(file, uid);
    }

    @ApiOperation("附件上传，支持批量上传")
    @RequestMapping("/batchUploadAttachment")
    @PreAuthorize("hasAnyAuthority('xinjianneirongjifabu','xiugaineirongjigengxin')")
    public Result batchUploadAttachment(@RequestParam(value = "files") MultipartFile[] files, @CurrentUser Users user) {
        return articleService.batchUploadAttachment(files);
    }

    @ApiOperation("附件删除")
    @RequestMapping("/deleteAttachment")
    @PreAuthorize("hasAnyAuthority('shanchuneirong','xinjianneirongjifabu','xiugaineirongjigengxin')")
    public Result deleteAttachment(@RequestBody AttachmentParam attachmentParam) {
        return articleService.deleteAttachment(attachmentParam);
    }

    @ApiOperation("删除回收站中的稿件")
    @PostMapping("deleteArticleFromRecycle")
    @PreAuthorize("hasAnyAuthority('gaojiankuhuishouzhanguanli')")
    public Result deleteArticleFromRecycle(@RequestBody ArticleRecycleParam param, @CurrentUser Users user) {
        if (param.getArticleIds() == null || param.getArticleIds().length == 0) {
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        return articleService.deleteArticleFromRecycle(param.getArticleIds());
    }

    @ApiOperation("恢复回收站中的稿件")
    @PostMapping("restoreArticleFromRecycle")
    @PreAuthorize("hasAnyAuthority('gaojiankuhuishouzhanguanli')")
    public Result restoreArticleFromRecycle(@RequestBody ArticleRecycleParam param, @CurrentUser Users user) {
        if (param.getArticleIds() == null || param.getArticleIds().length == 0) {
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        return articleService.restoreArticleFromRecycle(param.getArticleIds());
    }

    @ApiOperation("清空回收站中的稿件")
    @PostMapping("clearArticleFromRecycle")
    @PreAuthorize("hasAnyAuthority('gaojiankuhuishouzhanguanli')")
    public Result clearArticleFromRecycle(@CurrentUser Users user) {
        log.info("==============clearArticleFromRecycle============");
        return articleService.clearArticleFromRecycle();
    }

    @ApiOperation("关键词新增")
    @OperLog(operModule = NewsConstant.SYSTEM_MANAGE, operType = NewsConstant.ADD_OPERATION, operDesc = "添加文章关键词")
    @PostMapping("/keywords/add")
    @PreAuthorize("hasAnyAuthority('xinzengguanjianci')")
    public Result addArticleKeywords(@RequestBody List<ArticleKeywordParam> params, @CurrentUser Users user) {
        if (CollectionUtils.isEmpty(params)) {
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        return articleService.addArticleKeywords(params);
    }

    @ApiOperation("关键词删除")
    @OperLog(operModule = NewsConstant.SYSTEM_MANAGE, operType = NewsConstant.DELETE_OPERATION, operDesc = "删除文章关键词")
    @PostMapping("/keywords/delete")
    @PreAuthorize("hasAnyAuthority('shanchuguanjianci')")
    public Result deleteArticleKeywordByIds(@RequestBody ArticleKeywordParam param, @CurrentUser Users user) {
        if (CollectionUtils.isEmpty(param.getIds()) || StringUtils.isEmpty(param.getCapital())) {
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        return articleService.deleteArticleKeywordByIds(param);
    }

    @ApiOperation("关键词修改")
    @OperLog(operModule = NewsConstant.SYSTEM_MANAGE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "修改文章关键词")
    @PostMapping("/keywords/edit")
    @PreAuthorize("hasAnyAuthority('xiugaiguanjianci')")
    public Result editArticleKeywords(@RequestBody ArticleKeywordParam param, @CurrentUser Users user) {
        if (param.getId() == null) {
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        if (param.getKeyword().equals(param.getAlias())) {
            return Result.error(ResponseEnum.ALIAS_KEYWORD_LIKE);
        }

        return articleService.editArticleKeywords(param);
    }

    @ApiOperation("关键词查询")
    @PostMapping("/keywords/query")
    @PreAuthorize("hasAnyAuthority('chakanguanjianci')")
    public Result findArticleKeywords(@RequestBody ArticleKeywordParam param, @CurrentUser Users user) {
        return articleService.findArticleKeywords(param);
    }

    @ApiOperation("查询关键词被使用次数")
    @PostMapping("/keywords/num/query")
    @PreAuthorize("hasAnyAuthority('chakanguanjianci')")
    public Result<Integer> findArticleKeywordsNumQuery(@RequestBody ArticleKeywordParam param, @CurrentUser Users user) {
        if (param.getId() == null) {
            return Result.error(ResponseEnum.PARAM_ERROR);
        }

        return articleService.findArticleKeywordsNumQuery(param);
    }


}
